package me.sauce.folkpocket48.data.local.sp

import android.content.SharedPreferences
import android.preference.PreferenceManager
import android.text.TextUtils
import androidx.content.edit
import me.sauce.folkpocket48.App


/**
 * Created by sauce on 2017/2/4.
 * Version 1.0.0
 */
class LoginManager {
    private val KEY_TOKEN = "token"
    private val KEY_DEBUG = "debug"
    private val KEY_DEVICE_TOKEN = "deviceToken"

    private val KEY_MOBILE = "mobile"


    var token: String
        get() = sp.getString(KEY_TOKEN, "")
        set(token) = sp.edit { putString(KEY_TOKEN, token) }
    var isLogined = false
        get() = TextUtils.isEmpty(token)
    var isDebug: Boolean
        get() = sp.getBoolean(KEY_DEBUG, false)
        set(isDebug) = sp.edit().putBoolean(KEY_DEBUG, isDebug).apply()

    var deviceToken: String
        get() = sp.getString(KEY_DEVICE_TOKEN, null)
        set(deviceToken) = sp.edit { putString(KEY_DEVICE_TOKEN, deviceToken) }

    var mobile: String
        get() = sp.getString(KEY_MOBILE, null)
        set(mobile) = sp.edit { sp.edit { putString(KEY_MOBILE, mobile) } }


    fun exitLogin() {
        token = ""
    }


    companion object {
        private var sp: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(App.INSTANCE.get())


        fun get(): LoginManager {
            return Inner.singleton
        }
    }

    private object Inner {
        val singleton = LoginManager()
    }


}

